package com.atwy.business.service.Impl;

import com.atwy.business.service.BusinessService;
import com.atwy.common.seatatcc.OrderServiceTCCApi;
import com.atwy.common.seatatcc.StorageServiceTCCApi;
import io.seata.core.context.RootContext;
import io.seata.rm.tcc.api.BusinessActionContext;
import io.seata.spring.annotation.GlobalTransactional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class BusinessServiceImpl implements BusinessService {
     private static final Logger log = LoggerFactory.getLogger(BusinessServiceImpl.class);
    @Autowired
    OrderServiceTCCApi orderServiceTCCApi;
    @Autowired
    StorageServiceTCCApi storageServiceTCCApi;

    @GlobalTransactional
    @Override
    public boolean purchase(String userId, String commodityCode, int count) {
        String xid = RootContext.getXID();
        BusinessActionContext bac = new BusinessActionContext();
        bac.setXid(xid);
        log.info("设置全局事务编号：{}",xid);
        orderServiceTCCApi.prepare(bac, userId, commodityCode, count);
        storageServiceTCCApi.prepare(bac, commodityCode, count);
        log.info("购买成功");
        return true;
    }
}
